<script setup lang="ts">

import { loginAPI } from '@/apis/user'
import { REMEMBER_ME_KEY } from '@/constants'
import router from '@/router'
import { useUserStore } from '@/stores/user'
import type { FormInstance } from 'element-plus'
import Cookies from 'js-cookie'
import { onMounted, ref } from 'vue'
import { stringifyQuery } from 'vue-router'
const userStore = useUserStore()
const formData = ref({
  username:'',
  password:'',
  remember:false
})
const formRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    {  min: 3, message: '用户名长度不能小于3位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { pattern:/^[a-zA-Z0-9%]{6,15}$/, message: '密码长度6-15位', trigger: 'blur' }
  ],
}
// 表单实例
const form = ref<FormInstance>()

onMounted(() => { 
  const rememberDataStr = localStorage.getItem(REMEMBER_ME_KEY)
  console.log(rememberDataStr)
  if(rememberDataStr !== null){
    const userData = JSON.parse(rememberDataStr)
    formData.value = userData
  }
})

const login = async() => {
  try {
    await form.value!.validate()
    const {  username, password } = formData.value
    const res = await loginAPI({ username, password })

    // 为了确保输入的用户名密码正确发送请求成功之后才进行，将该功能放置该行
    // 记住我功能
    if(formData.value.remember){
      // Cookies.set(REMEMBER_ME_KEY,JSON.stringify(formData.value)) 或者
      localStorage.setItem(REMEMBER_ME_KEY,JSON.stringify(formData.value))
    }
    userStore.setToken(res.data.token)
    console.log(res)
    router.push('/')
    // console.log('登录成功')
  } catch (error) {
    console.error(error)
  }
  
}

</script>

<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>

      <el-form ref="form" :model="formData" :rules="formRules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username"/>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" show-password/>
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="formData.remember">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button @click="login" type="primary" class="login_btn">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login_body {
  display: flex;
}

.bg {
  width: 60vw;
  height: 100vh;
  background: url("@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}

.box {
  margin: 200px 10% 0;
  flex: 1;

  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }

  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }

    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }

    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}

.login_btn {
  width: 100%;
}
</style>
